package com.hmall.order.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.common.client.ItemServiceClient;
import com.hmall.common.client.UserClient;
import com.hmall.common.dto.Address;
import com.hmall.common.dto.Item;
import com.hmall.order.commone.BaseContext;
import com.hmall.order.mapper.OrderDetailMapper;
import com.hmall.order.mapper.OrderLogisticMapper;
import com.hmall.order.mapper.OrderMapper;
import com.hmall.order.pojo.Order;
import com.hmall.order.pojo.OrderDetail;
import com.hmall.order.pojo.OrderDto;
import com.hmall.order.pojo.OrderLogistics;
import com.hmall.order.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class OrderService extends ServiceImpl<OrderMapper, Order> implements IOrderService {

    @Autowired
    ItemServiceClient itemServiceClient;

     @Autowired
     OrderDetailMapper orderDetailMapper;

     @Autowired
     UserClient userClient;

     @Autowired
    OrderLogisticMapper orderLogisticMapper;
    /**
     * 提交订单
     * @param orderDto
     * @return
     */
    @Override
    public Long creatOrder(OrderDto orderDto) {
         //- 1）根据雪花算法生成订单id
        //- 2）商品微服务提供FeignClient，实现根据id查询商品的接口
        //- 3）根据itemId查询商品信息
        Long itemId = orderDto.getItemId();
        Item item = itemServiceClient.getById(itemId);
        //- 4）基于商品价格、购买数量计算商品总价：totalFee
        // 价格
        Long price = item.getPrice();
        // 数量
        Integer num = orderDto.getNum();
         //总价
        long totalFee = price * num;

        //- 5）封装Order对象，初识status为未支付
        Order order = new Order();
        order.setStatus(1);
        order.setTotalFee(totalFee);
        order.setPaymentType(orderDto.getPaymentType());
        order.setUserId(BaseContext.getCurrenId());
        //创建订单时间
        order.setCreateTime(new Date());
        //更新时间
        order.setUpdateTime(new Date());
        //- 6）将Order写入数据库tb_order表中
          this.save(order);
        //- 7）将商品信息、orderId信息封装为OrderDetail对象，写入tb_order_detail表
        OrderDetail orderDetail = new OrderDetail();
         orderDetail.setOrderId(order.getId());
         orderDetail.setItemId(orderDto.getItemId());
         orderDetail.setNum(orderDto.getNum());
         orderDetail.setTitle(item.getName());
         orderDetail.setSpec(item.getSpec());
         orderDetail.setImage(item.getImage());
         orderDetail.setCreateTime(new Date());
         orderDetail.setUpdateTime(new Date());
         orderDetail.setPrice(item.getPrice());
        orderDetailMapper.insert(orderDetail);
        //- 8）将user-service的根据id查询地址接口封装为FeignClient
        //- 9）根据addressId查询user-service服务，获取地址信息
        Address address = userClient.findAddressById(orderDto.getAddressId());

        //- 10）将地址封装为OrderLogistics对象，写入tb_order_logistics表
        OrderLogistics orderLogistics = new OrderLogistics();
            orderLogistics.setOrderId(order.getId());
            orderLogistics.setContact(address.getContact());
            orderLogistics.setPhone(address.getMobile());
            orderLogistics.setProvince(address.getProvince());
            orderLogistics.setCity(address.getCity());
            orderLogistics.setTown(address.getTown());
            orderLogistics.setStreet(address.getStreet());
            orderLogistics.setCreateTime(new Date());
            orderLogistics.setUpdateTime(new Date());
         orderLogisticMapper.insert(orderLogistics);
        //- 11）在item-service提供减库存接口，并编写FeignClient
        //- 12）调用item-service的减库存接口
        itemServiceClient.stock(orderDto.getItemId(),orderDto.getNum());
        return order.getId();
    }



}
